package org.soretro.portfolio.view.mainNavigation
{
	import com.google.analytics.AnalyticsTracker;
	import com.google.analytics.GATracker;
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	import com.greensock.plugins.ColorTransformPlugin;
	import com.greensock.plugins.TweenPlugin;
	
	import flash.events.MouseEvent;
	
	import mx.managers.SystemManager;
	
	import org.robotlegs.mvcs.Mediator;
	import org.soretro.portfolio.controller.events.MainNavigationEvent;
	import org.soretro.portfolio.helpers.GoogleAnalytics;
	import org.soretro.portfolio.model.MainNavigationModel;
	import org.soretro.portfolio.view.videoPlayer.events.VideoPlayerEvent;
	
	public class MainNavigationButtonMediator extends Mediator
	{
		[Inject]
		public var view:MainNavigationButtonView;
		
		[Inject]
		public var model:MainNavigationModel;
		
		public var tracker:AnalyticsTracker;
		
		override public function onRegister():void
		{
			googleTracker();
			
			TweenPlugin.activate([ColorTransformPlugin]);
			
			eventMap.mapListener( eventDispatcher, MainNavigationEvent.CLICKED, navigationChanged );
			eventMap.mapListener( eventDispatcher, MainNavigationEvent.STATE_CHANGE, stateChanged );
			
			model.pages[ view.bttnId ] = view.label;
			
			if ( model.getCurrentPage() == view.bttnId ) {
				view.currentState = "selected";
				TweenLite.to( view.bttnLabel, 0.3, { top:4, delay:0.8 } );
			} else {
				view.currentState = "mouseOut";
			}
			view.addEventListener( MouseEvent.MOUSE_OVER, updateView );
			view.addEventListener( MouseEvent.MOUSE_OUT, updateView );
			view.addEventListener( MouseEvent.MOUSE_DOWN, updateView );
			
			view.addEventListener( MouseEvent.CLICK, updateView );
		}
		
		private function googleTracker():void
		{
			tracker = new GATracker( SystemManager.getSWFRoot(this).stage, "UA-18699334-1", "AS3", false );
		}
		
		public function stateChanged( event:MainNavigationEvent ):void
		{
			view.currentState = "mouseOut";
			
			if ( model.getCurrentPage() != view.bttnId ) {
//				model.changePage( view.bttnId );
				TweenLite.to( view.bttnLabel, 0.5, { top:13, delay:0.8 } );
			} else {
//				Alert.show( "view.label = "+ view.label );
				view.currentState = "selected";
				TweenLite.to( view.bttnLabel, 0.3, { top:4, delay:0.8 } );
			}
		}
		
		private function navigationChanged( event:MainNavigationEvent = null ):void
		{
			view.currentState = "mouseOut";
			
			if ( model.getCurrentPage() != view.bttnId ) {
//				model.changePage( view.bttnId );
				TweenLite.to( view.bttnLabel, 0.5, { top:13, delay:0.8 } );
			} else {
				view.currentState = "selected";
				TweenLite.to( view.bttnLabel, 0.3, { top:4, delay:0.8 } );
			}
		}
		
		protected function updateView( event:MouseEvent ):void
		{
			switch( event.type )
			{
				case MouseEvent.MOUSE_OVER:
				{
//					Mouse.cursor = MouseCursor.BUTTON;
					if ( model.getCurrentPage() == view.bttnId ) {
						view.currentState = "selected";
					} else {
						view.currentState = "mouseOver";
						TweenMax.to( view.bttnLabel, 0.3, { alpha:0.6, yoyo:true, repeat:-1, repeatDelay:0.1, delay:0.1 } );
					}
					break;
				}
				case MouseEvent.MOUSE_OUT:
				{
//					Mouse.cursor = MouseCursor.ARROW;
					if ( model.getCurrentPage() == view.bttnId ) {
						view.currentState = "selected";
					} else {
						view.currentState = "mouseOut";
						TweenLite.to( view.bttnLabel, 0.5, { alpha:1, delay:0.1 } );
					}
					break;
				}
				case MouseEvent.MOUSE_DOWN:
				{
					if ( model.getCurrentPage() != view.bttnId ) {
						view.currentState = "mouseDown";
						TweenMax.to( view.bttnLabel, 0.3, { top:13 } );
					} else {
						view.currentState = "selected";
					}
					break;
				}
				case MouseEvent.CLICK:
				{
					TweenMax.to( view.bttnLabel, 0.3, { alpha:1 } );
					if ( model.getCurrentPage() != view.bttnId ) {
						view.currentState = "selected";
						GoogleAnalytics.getInstance().send( model.getPageName( view.bttnId ) );
//						tracker.trackPageview( model.getPageName( view.bttnId ) );
						
						dispatch( new VideoPlayerEvent( VideoPlayerEvent.HIDE_VIDEO ) );
						dispatch( new MainNavigationEvent( view.bttnId, MainNavigationEvent.CLICKED ) );
					}
					
					break;
				}
			}
		}
	}
}